Make DataOutputAgent use `events_order` for ordering events in the output

Akinori MUSHA 8 years ago
parent
commit
0eb38b0a94
3 changed files with 23 additions and 2 deletions
  1. 1 0
      CHANGES.md
  2. 6 2
      app/models/agents/data_output_agent.rb
  3. 16 0
      spec/models/agents/data_output_agent_spec.rb

+ 1 - 0
CHANGES.md

@@ -1,5 +1,6 @@
1 1
 # Changes
2 2
 
3
+* Jul 22, 2015   - DataOutputAgent can configure the order of events in the output via `events_order`.
3 4
 * Jul 20, 2015   - Control Links (used by the SchedularAgent) are correctly exported in Scenarios.
4 5
 * Jul 20, 2015   - keep\_events\_for was moved from days to seconds; Scenarios have a schema verison.
5 6
 * Jul 1, 2015    - DeDuplicationAgent properly handles destruction of memory.

+ 6 - 2
app/models/agents/data_output_agent.rb

@@ -40,11 +40,15 @@ module Agents
40 40
               "_contents": "tag contents (can be an object for nesting)"
41 41
             }
42 42
 
43
+        # Ordering events in the output
44
+
45
+        #{description_events_order('events in the output')}
46
+
43 47
         # Liquid Templating
44 48
 
45 49
         In Liquid templating, the following variable is available:
46 50
 
47
-        * `events`: An array of events being output, sorted in descending order up to `events_to_show` in number.  For example, if source events contain a site title in the `site_title` key, you can refer to it in `template.title` by putting `{{events.first.site_title}}`.
51
+        * `events`: An array of events being output, sorted in the given order, up to `events_to_show` in number.  For example, if source events contain a site title in the `site_title` key, you can refer to it in `template.title` by putting `{{events.first.site_title}}`.
48 52
 
49 53
       MD
50 54
     end
@@ -134,7 +138,7 @@ module Agents
134 138
         end
135 139
       end
136 140
 
137
-      source_events = received_events.order(id: :desc).limit(events_to_show).to_a
141
+      source_events = sort_events(received_events.order(id: :desc).limit(events_to_show).to_a)
138 142
 
139 143
       interpolation_context.stack do
140 144
         interpolation_context['events'] = source_events

+ 16 - 0
spec/models/agents/data_output_agent_spec.rb

@@ -209,6 +209,22 @@ describe Agents::DataOutputAgent do
209 209
         })
210 210
       end
211 211
 
212
+      describe 'ordering' do
213
+        before do
214
+          agent.options['events_order'] = ['{{title}}']
215
+        end
216
+
217
+        it 'can reorder the events_to_show last events based on a Liquid expression' do
218
+          asc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
219
+          expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving", "Evolving again", "Evolving yet again with a past date"])
220
+
221
+          agent.options['events_order'] = [['{{title}}', 'string', true]]
222
+
223
+          desc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
224
+          expect(desc_content['items']).to eq(asc_content['items'].reverse)
225
+        end
226
+      end
227
+
212 228
       describe "interpolating \"events\"" do
213 229
         before do
214 230
           agent.options['template']['title'] = "XKCD comics as a feed{% if events.first.site_title %} ({{events.first.site_title}}){% endif %}"